//
//  AnnotationViewController.m
//  APJ HRadar
//
//  Created by Richard Zhao on 3/5/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "AnnotationViewController.h"
#import "StaffCell.h"
#import "Staff.h"
#import "HRStore.h"
#import "StaffProfileViewController.h"
#import <CoreText/CoreText.h>
#import "Const.h"
#import "UIView+Toast.h"

@interface AnnotationViewController ()
{
    NSMutableArray *dataSource;
    NSArray *bookmarks;
}
@end

@implementation AnnotationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
//        UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:@"Annotatioin" image:[UIImage imageNamed:@"tab_annotation"] tag:4];
        
        UIView *backView = [[UIView alloc]initWithFrame:self.view.frame];
        backView.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
        self.tableView.backgroundView= backView;
        
        UITabBarItem *tab = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:4];
        self.tabBarItem = tab;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[self navigationItem] setTitle:@"My Bookmark"];
    UINib *nib = [UINib nibWithNibName:@"StaffCell" bundle:nil];
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"staff"];
    
    dataSource = [[NSMutableArray alloc] init];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    dataSource = [[NSMutableArray alloc] init];
    
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirector = [documentDirectories objectAtIndex:0];
    NSString *path =[documentDirector stringByAppendingPathComponent:BOOKMAKE_FILE_NAME];
    
    bookmarks = [NSArray arrayWithContentsOfFile:path];
    
    if (bookmarks && bookmarks.count > 0) {
        for (NSNumber *staffId in bookmarks) {
            Staff *staff = [[HRStore sharedStore] findStaffById:staffId.intValue];
            [dataSource addObject:staff];
        }
    } else {
        [self.view makeToast:@"No staff in bookmark yet."
                    duration:3.0
                    position:[NSValue valueWithCGPoint:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height - 220)]];
    }
    
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return bookmarks.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Staff *staff = [dataSource objectAtIndex:[indexPath row]];
    
    StaffCell *cell = [tableView dequeueReusableCellWithIdentifier:@"staff"];
    if(cell==nil){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"StaffCell" owner:self options:nil] lastObject];
    }
    [[cell nameLabel] setText:[staff chineseName]];
    [[cell titleLabel] setText:[staff jobTitle]];
    NSMutableAttributedString *mail = [[NSMutableAttributedString alloc] initWithString:[staff companyEmail]];
    [mail addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                 value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
                 range:(NSRange){0, mail.length}];
    [[cell emailLabel] setAttributedText:mail];
    [[cell remarksText] setText:[[HRStore sharedStore] allSkillsStringOfStaff:[staff staffId]]];
    
    
    NSString *imgPath = [NSString stringWithFormat:@"%d.png", staff.staffId];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:imgPath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[cell photoImage] setImage:[UIImage imageWithContentsOfFile:path]];
    } else {
        [[cell photoImage] setImage:[UIImage imageNamed:@"defaultPhoto"]];
    }
    
    cell.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 175.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StaffProfileViewController *staffPro = [[StaffProfileViewController alloc] init];
    [staffPro setStaff:[dataSource objectAtIndex:[indexPath row]]];
    [[staffPro navigationItem] setTitle:@"Staff Detail Info."];
    [[self navigationController] pushViewController:staffPro animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
